package com.dzl.fee;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareFinanceEnum;
import com.dzl.finance.pojo.query.WelfareFinancePaymentQuery;
import com.dzl.finance.pojo.vo.WelfareFinancePaymentVo;
import com.dzl.finance.service.WelfareFinancePaymentService;
import com.dzl.order.pojo.dto.WelfareOrderBatchUpdateDto;
import com.dzl.order.service.WelfareOrderService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 财务打款审核控制层
 * @date 2021/10/12 11:11
 * @author liuguanjun
 */
@RestController
@RequestMapping("/financialPayment")
public class FinancialPaymentController extends BaseController {

    @Autowired
    private WelfareFinancePaymentService welfareFinancePaymentService;
    @Autowired
    private WelfareOrderService welfareOrderService;

    /**
     * 查询财务打款顶部状态栏数量
     * @date 2021/10/12 16:38
     * @author liuguanjun
     * @param query 查询条件
     */
    @GetMapping("/queryCountByPaymentStatus")
    public JsonResult<List<Integer>> getWelfareFinancePaymentCount(WelfareFinancePaymentQuery query) {
        query.setEnterpriseId(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        return JsonResult.success(welfareFinancePaymentService.getWelfareFinancePaymentCount(query));
    }

    /**
     * 财务打款审核分页查询
     * @date 2021/10/12 16:27
     * @author liuguanjun
     * @param query 查询条件
     */
    @GetMapping("/queryPageList")
    public PageUtils<WelfareFinancePaymentVo> querySalesFlowVoListPage(WelfareFinancePaymentQuery query) {
        query.setEnterpriseId(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        PageInfo<WelfareFinancePaymentVo> pageInfo = welfareFinancePaymentService.getWelfareFinancePaymentListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 财务打款批量打款或单个打款
     * @date 2021/10/12 16:38
     * @author liuguanjun
     * @param param 订单id等参数
     */
    @PostMapping("/doPayment")
    public JsonResult<Integer> doPayment(@RequestBody JSONObject param) {
        String orderIds = param.getString("orderIds"); // 订单id，多个订单中间逗号分隔
        if(EmptyUtils.isEmpty(orderIds)) {
            return JsonResult.paramErrWithMsg("传参有误:orderIds不能传空");
        }
        WelfareOrderBatchUpdateDto welfareOrderBatchUpdateDto = new WelfareOrderBatchUpdateDto();
        welfareOrderBatchUpdateDto.setIds(orderIds);
        welfareOrderBatchUpdateDto.setUpdater(getAdminInfo().getId());
        welfareOrderBatchUpdateDto.setUpdaterName(getAdminInfo().getUserName());
        welfareOrderBatchUpdateDto.setPaymentStatus(WelfareFinanceEnum.FINANCE_PAYMENT_STATUS_PAID.getCode());
        return JsonResult.success(welfareOrderService.batchUpdateOrderByIds(welfareOrderBatchUpdateDto));
    }

    /**
     * 财务打款批量驳回
     * @date 2021/10/12 16:38
     * @author liuguanjun
     * @param param 订单id等参数
     */
    @PostMapping("/batchRejection")
    public JsonResult<Integer> batchRejection(@RequestBody JSONObject param) {
        String orderIds = param.getString("orderIds");//订单id，多个订单中间逗号分隔
        if(EmptyUtils.isEmpty(orderIds)) {
            return JsonResult.paramErrWithMsg("传参有误:orderIds不能传空");
        }
        WelfareOrderBatchUpdateDto welfareOrderBatchUpdateDto = new WelfareOrderBatchUpdateDto();
        welfareOrderBatchUpdateDto.setIds(orderIds);
        welfareOrderBatchUpdateDto.setUpdater(getAdminInfo().getId());
        welfareOrderBatchUpdateDto.setUpdaterName(getAdminInfo().getUserName());
        welfareOrderBatchUpdateDto.setPaymentStatus(WelfareFinanceEnum.FINANCE_PAYMENT_STATUS_REJECTED.getCode());
        return JsonResult.success(welfareOrderService.batchUpdateOrderByIds(welfareOrderBatchUpdateDto));
    }

    /**
     * 财务打款上传回执单
     * @date 2021/10/12 16:38
     * @author liuguanjun
     * @param param 订单id等参数
     */
    @PostMapping("/batchUploadPaymentFile")
    public JsonResult<Integer> batchUploadPaymentFile(@RequestBody JSONObject param) {
        String orderIds = param.getString("orderIds");  //订单id，多个订单中间逗号分隔
        String paymentFile = param.getString("paymentFile"); //回执单地址
        if(EmptyUtils.isEmpty(orderIds)) {
            return JsonResult.paramErrWithMsg("传参有误:orderIds不能传空");
        }
        if(EmptyUtils.isEmpty(paymentFile)) {
            return JsonResult.paramErrWithMsg("传参有误:paymentFile不能传空");
        }
        WelfareOrderBatchUpdateDto welfareOrderBatchUpdateDto = new WelfareOrderBatchUpdateDto();
        welfareOrderBatchUpdateDto.setIds(orderIds);
        welfareOrderBatchUpdateDto.setPaymentFile(paymentFile);
        return JsonResult.success(welfareOrderService.batchUpdateOrderByIds(welfareOrderBatchUpdateDto));
    }

    /**
     * 批量导出打款订单数据
     * @date 2021/10/14 17:36
     * @author liuguanjun
     * @param param orderIds 订单id，多个订单中间逗号分隔
     */
    @PostMapping("/batchExportPaymentInfo")
    public void batchExportPaymentInfo(@RequestBody JSONObject param, HttpServletResponse res) {
        String orderIds = param.getString("orderIds");
        if(EmptyUtils.isEmpty(orderIds)) {
            throw new CommonException("传参有误:orderIds不能传空");
        }
        welfareFinancePaymentService.batchExportPaymentInfo(orderIds,res);
    }

}
